@echo off
cd frontend

REM 获取命令行参数，默认值为localhost:3000
set HOST=localhost
set PORT=3000
set BACKEND_HOST=localhost
set BACKEND_PORT=8000

REM 解析命令行参数
:parse_args
if "%~1"=="" goto :run
if "%~1"=="--host" (
    set HOST=%~2
    shift
    shift
    goto :parse_args
)
if "%~1"=="--port" (
    set PORT=%~2
    shift
    shift
    goto :parse_args
)
if "%~1"=="--backend-host" (
    set BACKEND_HOST=%~2
    shift
    shift
    goto :parse_args
)
if "%~1"=="--backend-port" (
    set BACKEND_PORT=%~2
    shift
    shift
    goto :parse_args
)
shift
goto :parse_args

:run
echo 启动前端服务器，监听 %HOST%:%PORT%，后端地址 %BACKEND_HOST%:%BACKEND_PORT%

REM 设置Vite所需的环境变量并启动
set "VITE_HOST=%HOST%"
set "VITE_PORT=%PORT%"
set "VITE_BACKEND_HOST=%BACKEND_HOST%"
set "VITE_BACKEND_PORT=%BACKEND_PORT%"

REM 使用直接命令行代替npm运行，以确保环境变量传递正确
npx vite --host %HOST% --port %PORT%
